package cn.afactory.tygz.ui.base.views;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

import java.util.EnumMap;

import cn.afactory.tygz.R;

/**
 * Created by Kenneth on 15/9/12.
 */
public class NearByPeopleFilterBar extends RelativeLayout {

    public enum FilterType {
        NONE,
        GENDER,
        AGE,
        HOMETOWN,
    }

    public interface IListDataEntity {
        public String getDisplayText();
        public long getId();
    }

    public EnumMap<FilterType, IListDataEntity> mFilterValueMap = new EnumMap<FilterType, IListDataEntity>(FilterType.class);

    @ViewInject(R.id.filter_type_region)
    private ViewGroup mGenderPanel;
    @ViewInject(R.id.tv_type_region)
    private TextView mGenderText;
    @ViewInject(R.id.arrow_type_region)
    private ImageView mGenderImg;

    // department
    @ViewInject(R.id.filter_type_department)
    private ViewGroup mAgePanel;
    @ViewInject(R.id.tv_type_department)
    private TextView mAgeText;
    @ViewInject(R.id.arrow_type_department)
    private ImageView mAgeImg;

    // hospital
    @ViewInject(R.id.filter_type_hospital)
    private ViewGroup mHomeTownPanel;
    @ViewInject(R.id.tv_type_hospital)
    private TextView mHomeTownText;
    @ViewInject(R.id.arrow_type_hospital)
    private ImageView mHomeTownImg;

    private FilterType mLastFilterType = FilterType.NONE;
    private OnFilterClickListener mClickListener;

    public NearByPeopleFilterBar(Context context) {
        super(context);
        initView();
    }

    public NearByPeopleFilterBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public NearByPeopleFilterBar(Context context, AttributeSet attrs,
                                 int defStyle) {
        super(context, attrs, defStyle);
        initView();
    }


    private void initView() {
        View view = inflate(getContext(),
                R.layout.view_nearby_people_filterbar, this);
        ViewUtils.inject(view);
    }

    @OnClick({R.id.filter_type_region, R.id.filter_type_department,
            R.id.filter_type_hospital})
    public void onFilterClick(View view) {
        FilterType type = null;
        switch (view.getId()) {
            case R.id.filter_type_region:
                type = FilterType.GENDER;
                break;
            case R.id.filter_type_department:
                type = FilterType.AGE;
                break;
            default:
                type = FilterType.HOMETOWN;
                break;
        }

        updateView(type);

        if (mClickListener != null) {
            mClickListener.onFilterClick(mLastFilterType);
        }
    }

    public void performFilter(FilterType type) {
        switch (type) {
            case GENDER:
                mGenderPanel.performClick();
                break;
            case AGE:
                mAgePanel.performClick();
                break;
            default:
                mHomeTownPanel.performClick();
                break;
        }
    }

    private void updateView(FilterType selectType) {
        if (mLastFilterType == selectType) {
            mLastFilterType = FilterType.NONE;
        } else {
            mLastFilterType = selectType;
        }
        mGenderText.setSelected(FilterType.GENDER == mLastFilterType);
        mGenderImg.setSelected(FilterType.GENDER == mLastFilterType);

        mAgeText.setSelected(FilterType.AGE == mLastFilterType);
        mAgeImg.setSelected(FilterType.AGE == mLastFilterType);

        mHomeTownText.setSelected(FilterType.HOMETOWN == mLastFilterType);
        mHomeTownImg.setSelected(FilterType.HOMETOWN == mLastFilterType);
    }

    public void setFilterValueByType(FilterType type, IListDataEntity value) {
        mFilterValueMap.put(type, value);
        if(value!=null){
            updateFilterTextByType(type, value);
        }
    }

    private void updateFilterTextByType(FilterType type, IListDataEntity value) {
        switch (type) {
            case GENDER:
                mGenderText.setText(value.getDisplayText());
                break;
            case AGE:
                mAgeText.setText(value.getDisplayText());
                break;
            default:
                mHomeTownText.setText(value.getDisplayText());
                break;
        }
    }

    public void setFilterCancel() {
        mLastFilterType = FilterType.NONE;
        updateView(mLastFilterType);
    }

    public IListDataEntity getFilterValueByType(FilterType type) {
        return mFilterValueMap.get(type);
    }

    public void setOnFilterClickListener(OnFilterClickListener l) {
        mClickListener = l;
    }

    public interface OnFilterClickListener {
        public void onFilterClick(FilterType type);
    }

}
